package com.atguigu.springcloudalibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class FlowLimitController {

    private static final Logger log = LoggerFactory.getLogger(FlowLimitController.class);

    @GetMapping(value = "/testA")
    public String testA() {
     /*   try {
            TimeUnit.MILLISECONDS.sleep(800);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        return "--------------->testA";
    }

    @GetMapping(value = "/testB")
    public String testB() {
        //测试排队等待
        log.info(Thread.currentThread().getName() + "\t" + "----->testB");
        return "--------------->testB";
    }

    /**
     * 测试  rt
     *
     * @return
     */
    @GetMapping(value = "/testD")
    public String testD() {
        try {
            TimeUnit.MILLISECONDS.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "--------------->测试RT  testD";
    }

    /**
     * 测试异常比例
     *
     * @return
     */
    @GetMapping(value = "/testE")
    public String testE() {
        log.info("测试异常比例");
        int a = 10 / 0;
        return "--------------->测试异常比例  testE";
    }

    /**
     * 测试异常数
     *
     * @return
     */
    @GetMapping(value = "/testF")
    public String testF() {
        log.info("测试异常数");
        int a = 10 / 0;
        return "--------------->测试异常数  testF";
    }

    /**
     * 测试热点key
     * SentinelResource  只管配置时出的错误
     * 运行时的异常不管  还会照样报错
     * @return
     */
    @GetMapping(value = "/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey")
    public String testHotKey(@RequestParam(value = "p1",required = false)String p1,
                             @RequestParam(value = "p2",required = false)String p2) {
        log.info("测试热点key");
        return "--------------->测试热点key  testHotKey";
    }
    /**
     * 热点key兜底方法
     */
    public String deal_testHotKey(String p1, String p2, BlockException exception){
        return "------deal_testHotKey,o(╥﹏╥)o";
    }
}
